<template>
  <view>
    <!-- 搜索区域 -->
    <view class="search-box"><my-search @click.native="gotoSearch"></my-search></view>
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <!-- 循环渲染轮播图的 item 项 -->
      <swiper-item v-for="(item, index) in swiperList" :key="item.goods_id">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <!-- 动态绑定图片的 src 属性 -->
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, index) in navList" :key="index" @click="navListHandler(item)"><image :src="item.image_src"></image></view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <image :src="item.floor_title.image_src"></image>
        <!-- 图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧图片 -->
          <view class="left-img-box">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
              @click="goToGoodsList(item.product_list[0])"
            ></image>
          </view>
          <!-- 右侧图片 -->
          <view class="right-img-box">
            <!-- 双重循环时，命名一定要区分开，否侧会导致微信小程序报错 -->
            <image
              :src="item.image_src"
              :style="{ width: item.image_width + 'rpx' }"
              v-if="subIndex > 0"
              v-for="(item, subIndex) in item.product_list"
              mode="widthFix"
              :key="subIndex"
              @click="goToGoodsList(item)"
            ></image>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'PageHome',
  components: {},
  data() {
    return {
      swiperList: [], //轮播图数据
      navList: [], //分类导航数据
      floorList: [] //楼层数据列表
    };
  },
  created() {},
  mounted() {},
  onLoad() {
    this.getNavList(), this.getSwiperList(), this.getFloorList();
  },
  methods: {
    gotoSearch(){
      uni.navigateTo({
        url: '/subpkg/search/search'
      });
    },
    //点击跳转分类
    goToGoodsList(item) {
      uni.navigateTo({
        url: '/subpkg/goods_list/goods_list' + item.navigator_url.substr(item.navigator_url.indexOf('?'))
      });
    },
    //点击分类导航跳转
    navListHandler(item) {
      if ((item.name = '分类')) {
        uni.switchTab({
          url: '/pages/cate/cate'
        });
      }
    },
    //获取楼层数据
    async getFloorList() {
      let {
        data: {
          message,
          meta: { status }
        }
      } = await uni.$http.get('/api/public/v1/home/floordata');
      if (status !== 200) {
        return uni.$showMsg();
      }
      this.floorList = message;
    },
    //获取分类导航数据
    async getNavList() {
      let {
        data: {
          message,
          meta: { status }
        }
      } = await uni.$http.get('/api/public/v1/home/catitems');
      if (status !== 200) {
        return uni.$showMsg();
      }
      this.navList = message;
    },
    //获取导航栏数据
    async getSwiperList() {
      let {
        data: {
          message,
          meta: { status }
        }
      } = await uni.$http.get('/api/public/v1/home/swiperdata');
      if (status !== 200) {
        return uni.$showMsg();
      }
      this.swiperList = message;
    }
  }
};
</script>

<style lang="scss">
swiper {
  height: 330rpx;
  .swiper-item {
    height: 100%;
    image {
      width: 100%;
      height: 100%;
    }
  }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;
  .nav-item {
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }
}
.floor-item {
  image {
    height: 60rpx;
    display: flex;
  }
}
.floor-img-box {
  display: flex;
  padding-left: 10rpx;
  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
}
.search-box {
  position: sticky;
  top: 0px;
  z-index: 999;
}
</style>
